#include "Game.h"

#include "Input.h"
#include "Keyboard Key Index.h"

#include "GL/glut.h"
#include <GL/gl.h>
#include <GL/glu.h>

#include "IL/ilut.h"
#include "IL/il.h"
#include "IL/ilu.h"

#include <time.h>

/* ***********************************************************
// Global Variables
*********************************************************** */
// Error checking
unsigned int g_error = glGetError();

// Timer
clock_t clock_ticks;
Timer input_timer;
Timer physics_timer;
Timer display_timer;

// The current camera used to display
Camera* current_camera = new Camera();

// List of Models
const Model ShipModel	("Sample Ship.obj");
const Model PlanetModel ("Sample Planet.obj");

// List of Textures
// const Texture2D testTex ("Textures/level1.png");

// Entity List
std::vector<DrawnEntity*> draw_list;

/* ***********************************************************
// Function Prototypes
*********************************************************** */

void InitializeGame();
void InitializeDevIL();
void DrawGame();
void UpdateGame();
void KeyboardInputUpdate();
void MouseInputUpdate();

/* ***********************************************************
// Functions
*********************************************************** */

void InitializeDevIL() {
	ilutRenderer(ILUT_OPENGL);
	ilInit();
	iluInit();
	ilutInit();
	ilutRenderer(ILUT_OPENGL);
}

void InitializeGame() {
	/* Initialising Buttons */
	/* ******************** */
	UI_Button *tempbutton;

	tempbutton = new UI_Button();
	draw_list.push_back(tempbutton);
	
	//tempbutton
	tempbutton->orientation.set(0, 0, -1);
	tempbutton->MoveTo(0, 0, 0);
	tempbutton->scale = 1;
	tempbutton->width = 10;
	tempbutton->height = 2;

	/* Initialising Physical entities */
	/* ****************************** */
	PhysicalEntity * temp;

	// Creating Entity and Storing
	temp = new PhysicalEntity(&ShipModel);
	draw_list.push_back(temp);
	// Altering initial values of Entity
	temp->minimum_camera_distance = 1;
	temp->MoveTo(-10, 10, -5);
	temp->scale = 1/30.f;

	// Creating Entity and Storing
	temp = new PhysicalEntity(&PlanetModel);
	draw_list.push_back(temp);
	// Altering initial values of Entity
	temp->minimum_camera_distance = 6160;
	temp->MoveTo(0,0,-8000);
	temp->scale = 240;

	/* ***************************************************/
	/* Initialising Camera */
	/* ******************* */
	current_camera->MoveTo(0,0,5);
	current_camera->LookAt(draw_list[0]);
	g_error = glGetError();
}

void DrawGame() {
	g_error = glGetError();

	current_camera->PlaceCamera(); // NOTE:: This HAS to be run before the following for loop, no idea why
	/* Drawing Physical entities */
	for (int i = 0; i < (int)draw_list.size(); i++)
		draw_list[i]->Draw();
	
	g_error = glGetError();
}

void UpdateGame() {
	/* Updating input */
	KeyboardInputUpdate();
	MouseInputUpdate();

	/* Updating Camera */
	current_camera->Update();

	/* Updating entities */

}

void KeyboardInputUpdate() {
	static int focus_index = 0;

	/* Game update frame here */
	if (Keyboard.isKeyDown('W'))	{ current_camera->CameraOrbitVertical(2); }
	if (Keyboard.isKeyDown('S'))	{ current_camera->CameraOrbitVertical(-2); }
	if (Keyboard.isKeyDown('A'))	{ current_camera->CameraOrbitHorizontal(-2); }
	if (Keyboard.isKeyDown('D'))	{ current_camera->CameraOrbitHorizontal(2); }
	if (Keyboard.isKeyDown('Q'))	{ current_camera->Roll(-2); }
	if (Keyboard.isKeyDown('E'))	{ current_camera->Roll(2); }
	if (Keyboard.isKeyDown('F'))	{ current_camera->Zoom(-0.025); }
	if (Keyboard.isKeyDown('R'))	{ current_camera->Zoom(0.025); }

	if (Keyboard.isKeyDown('1'))
		current_camera->SlideTo(draw_list[0]); 
	
	if (Keyboard.isKeyDown('2'))
		current_camera->SlideTo(draw_list[1]);
	
	if (Keyboard.isKeyDown('3'))
		current_camera->SlideTo(draw_list[2]);

	/*if (Keyboard.isKeyDown('1') && focus_index > 0)	{
		focus_index--;
		current_camera->SlideTo(draw_list[focus_index]); 
	}
		
	if (Keyboard.isKeyDown('2') && focus_index < (int)draw_list.size() -1 )	{
		focus_index++;
		current_camera->SlideTo(draw_list[focus_index]);
	}*/
}

void MouseInputUpdate() {
	if (Mouse.isRightDown)			{ current_camera->RotateAround(10, &Mouse); }
	if (Mouse.isMiddleDown)			{ current_camera->SlideTo(&Mouse, draw_list); } 
	if (abs(Mouse.scroll_delta) > 0) { current_camera->VelocityZoom(Mouse.scroll_delta * 1.5); }
}